<?php

namespace App\Api;

use PhalApi\Api;


/**
 * 默认读取，更新，删除
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class IndexAd extends Api {

	public function getRules() {
		return array(
			'select'       => array(
				'page'  => array( 'name' => 'page', 'require' => true,  'desc' => '当前页' ),
				'size'  => array( 'name' => 'size', 'require' => true,  'desc' => '每页显示多少' ),
				'table' => array( 'name' => 'table', 'require' => false, 'defualt'=>'rok_account',  'desc' => '表名' ),
			),
			"update"       => array(
				'id'    => array( 'name' => 'id', 'require' => false, 'desc' => 'id' ),
				'arr'   => array( 'name' => 'arr', 'require' => true, 'desc' => '数据json数据' ),
				'table' => array( 'name' => 'table', 'require' => true, 'desc' => '表名' ),
			),
			"delete"       => array(
				'id'    => array( 'name' => 'id', 'require' => false, 'desc' => 'id' ),
				'table' => array( 'name' => 'table', 'require' => true, 'desc' => '表名' ),
			),
			"select_where" => array(
				'where' => array( 'name' => 'where', 'require' => true, 'desc' => '查询条件' ),
				'table' => array( 'name' => 'table', 'require' => true, 'desc' => '表名' ),
				'page'  => array( 'name' => 'page', 'require' => true, 'desc' => '当前页' ),
				'size'  => array( 'name' => 'size', 'require' => true, 'desc' => '每页显示多少' ),
			)
		);
	}

	/**
	 * 通用多条件查询接口
	 * @desc 多条件查询接口
	 * @return array data 被查数据
	 * @exception 400 非法请求，参数传递错误
	 */
	public function select_where() {
		header( 'Access-Control-Allow-Origin:*' );
        $page = $this->page;
        $size = $this->size;
        $where = json_decode($this->where,true);
        $newwhere = array();
        foreach ($where as $key=>$value){
            if($value){
                $newwhere[$key]=$value;
            }
        }
        $table = $this->table;


        $newwhere['imei_id'] = 88;
        // 按分页取数据
        $data = \PhalApi\DI()->notorm->$table
            ->where($newwhere)
            ->order("qus ASC")
            ->limit(($page - 1) * $size, $size);
        // 读出总数
        $count = \PhalApi\DI()->notorm->$table
            ->where($newwhere)
            ->count("id");
        $arr = array(
            'count' => $count,
            'data' => $data,
        );
        return $arr;
	}

	/**
	 * 删除接口
	 * @desc 更新id删除接口
	 * @return array data 删除状态
	 * @exception 400 非法请求，参数传递错误
	 */
	public function delete() {
		header( 'Access-Control-Allow-Origin:*' );
		return \App\deleteById( $this );
	}

	/**
	 * 更新及创建->接口
	 * @desc 更新及新建接口服务
	 * @return array data 数据内容
	 * @return int count 当前条件下总数
	 * @exception 400 非法请求，参数传递错误
	 */
	public function update() {
		header( 'Access-Control-Allow-Origin:*' );
		return \App\updateById( $this );
	}

	/**
	 * 分页读取
	 * @desc 按分页读取数据接口服务
	 * @return array data 数据内容
	 * @return int count 当前条件下总数
	 * @exception 400 非法请求，参数传递错误
	 */
	public function select() {
    // 最简单的处理方式
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: *');

//        header('Access-Control-Allow-Headers:x-requested-with,content-type,requesttype');
		return \App\getInfoByPage( $this );
	}

}
